<template>
  <div class="todo-list">
    <h2>自律一下</h2>
    <div class="input-container">
      <input v-model="newTodoTitle" placeholder="添加新的待办事项" @keyup.enter="addTodo" />
      <button @click="addTodo">添加</button>
    </div>
    <ul>
      <li v-for="(todo, index) in todos" :key="index">
        <TodoItem
          :title="todo.title"
          :completed="todo.completed"
          @update:completed="updateTodoCompleted(index, $event)"
          @remove-todo="removeTodo(index)"
        />
      </li>
    </ul>
  </div>
</template>

<script>
import TodoItem from './TodoItem.vue';

export default {
  name: 'TodoList',
  components: {
    TodoItem,
  },
  data() {
    return {
      todos: [],
      newTodoTitle: '',
    };
  },
  computed: {
  },
  methods: {
    addTodo() {
      if (this.newTodoTitle.trim()) {
        this.todos.push({ title: this.newTodoTitle, completed: false });
        this.newTodoTitle = '';
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
    updateTodoCompleted(index, completed) {
      this.todos[index].completed = completed;
    },
  },
};
</script>

<style scoped>
.todo-list {
  max-width: 400px;
  margin: 0 auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.input-container {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

input {
  flex: 1;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
}

button {
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-left: 10px; /* 添加间距 */
}

button:hover {
  background-color: #3aa271;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  margin-bottom: 10px;
}

/* 删除: 统计信息样式 */
/* 删除: .completed-count { */
/* 删除:   margin-top: 20px; */
/* 删除:   font-weight: bold; */
/* 删除:   color: #333; */
/* 删除: } */
</style>

